#include "rdr.h"
#include "nmfield.h"
#include "nmfield.cpp"

#include "nmarrfield.h"
#include "nmarrfield.cpp"

#include "stdffield.h"
#include "stdffield.cpp"

using namespace std;
using namespace opsrc::stdf;

Rdr::Rdr()
{
}

Rdr::Rdr(stdf_rec _stdfrec, int _reclen, std::ios::pos_type _fileRecFirstBytePosition, int _orderindex, endian e, char* buf)
:RecBase(_stdfrec, _reclen, _fileRecFirstBytePosition, _orderindex),en(e) {
    if (buf!=0)
        readFields(buf);
}

Rdr::~Rdr() {
}


const StdfField<int> Rdr::getRetestBinCount() const {
    return num_bins;
}

void Rdr::setRetestBinCount(StdfField<int> numbins) {
    num_bins = numbins;
}

const StdfField<vector<unsigned int> > Rdr::getRetestBinNumber() const {
    return rtst_bin;
}

void Rdr::setRetestBinNumber(StdfField<vector<unsigned int> > rtstbin) {
    rtst_bin = rtstbin;
}

void Rdr::readFields(char *buf) {
#if _DEBUG
    cout << "stdfrec= RDR , filebytestartposition= " <<   getFileRecByteStartPosition() << ", recbytecount= " << reclen <<endl;
#endif

    int curpos = 0;
    // Read num of bins count , 2 bytes
    NmField<unsigned int, 2> numbins(buf+curpos, en);
    num_bins.set(numbins.getData(), curpos, numbins.getByteCount(), numbins.getTotalByteCount());
    curpos += num_bins.getTotalByteCount();

    // Read retest bin number , array
    if (curpos < reclen-1) {
        NmArrField<unsigned int, 2> rtstbin(buf+curpos, en, num_bins.getData());
        rtst_bin.set(rtstbin.getData(), curpos, rtstbin.getByteCount(), rtstbin.getTotalByteCount());
    }
}

